package com.tianji.promotion.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponFormDTO;
import com.tianji.promotion.domain.dto.CouponIssueFormDTO;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.domain.query.CouponQuery;
import com.tianji.promotion.domain.vo.CouponPageVO;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.ICouponService;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 前端控制器
 * </p>
 *
 * @author hrz
 * @since 2025-09-01
 */
@RestController
@RequestMapping("/coupons")
@RequiredArgsConstructor
public class CouponController {
    private final ICouponService couponService;

    @PostMapping
    @ApiOperation("新增优惠券")
    public void addNewCoupon(@Validated @RequestBody CouponFormDTO couponFormDTO) {
        couponService.addNewCoupon(couponFormDTO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询优惠券")
    public PageDTO<CouponPageVO> queryCouponPage(@RequestBody CouponQuery query) {
        return couponService.queryCouponPage(query);
    }

    @PutMapping("/{id}/issue")
    @ApiOperation("发放优惠券")
    public void issueCoupon(@PathVariable Long id,
                            @Validated @RequestBody CouponIssueFormDTO couponIssueDTO) {
        couponService.issueCoupon(id, couponIssueDTO);
    }

    @GetMapping("/list")
    @ApiOperation("查询发放中的优惠券列表")
    public List<CouponVO> queryCouponIssuing(){
        return couponService.queryCouponIssuing();
    }

    @PutMapping("/{id}")
    @ApiOperation("修改优惠券")
    public void updateCoupon(@PathVariable Long id,
                             @Validated @RequestBody CouponFormDTO couponFormDTO) {
        couponService.updateCoupon(id, couponFormDTO);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除优惠券")
    public void deleteCoupon(@PathVariable Long id) {
        couponService.deleteCoupon(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询优惠券")
    public Coupon queryCoupon(@PathVariable Long id) {
        return couponService.getById(id);
    }

    @PutMapping("/{id}/pause")
    @ApiOperation("暂停发放优惠券")
    public void pauseCoupon(@PathVariable Long id) {
        couponService.pauseCoupon(id);
    }
}
